package com.qhgrain.app.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.pubframework.common.util.Props;
import com.qhgrain.app.constant.UrlEnum;

/**
 * 获取配置文件的数据
 * 如果操作系统是window，则读取配置文件，否则读取系统环境（看appFactory平台自定义环境变量）
 * @author Jfei
 *
 */
public class ConfigUtil {

	private static Logger LOG = LoggerFactory.getLogger(ConfigUtil.class);
	public static Props pro = null;
	private final static String config_file = "/apiurl.properties";
	public static int ostype = 0;
	
	private static void loadEnv() {
	    String osname = System.getProperty("os.name");
	    System.out.println("操作系统名称："+osname);
	    if ((osname != null) && (osname.toLowerCase().startsWith("win")))
	      ostype = 1;
	    else {
	      ostype = 2;
	    }
	    if (ostype == 1) {
	    	
	      try {
	       // String configfile = ContextLoader.getCurrentWebApplicationContext().getServletContext().getRealPath("/") + "/WEB-INF/config/env.properties";
	        LOG.info(config_file);
	      //  pro.load(new FileInputStream(config_file));
	        
	        if(pro == null){
	    		pro = new Props(config_file, ConfigUtil.class);
			}
	        
	      } catch (Exception e) {
	        pro = null;
	        e.printStackTrace();
	        LOG.error("load "+config_file+" error", e);
	      }
	    }
	  }

	  public static String getProperties(UrlEnum urlEnum) {
	    return getProperties(urlEnum.getCode());
	  }
	  
	  public static String getProperties(String key){
		  if (ostype == 0) {
		      loadEnv();
		    }
		    String val = null;
		    if (ostype == 1)
		      val = pro.getProperty(key);
		    else {
		      val = System.getenv(key);
		    }
		    
		    if(val == null || "".equals(val.trim())){
		    	LOG.error("获取配置参数"+key+" 失败...");
		    }
		    System.out.println("获取配置参数"+key+"="+val);
		    return val!=null?val.trim():null;
	  }
}
